package com.bjsxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.pojo.Company;
import com.bjsxt.mapper.CompanyMapper;
import com.bjsxt.pojo.PageResult;
import com.bjsxt.service.ICompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-02-22
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {
    //声明mapper层属性
    @Autowired
    private CompanyMapper companyMapper;
    @Override
    public PageResult<Company> selCompanyInfoService(Integer page, Integer rows, String cname, String status, String ordernumber) {
        //1、创建分页对象存储分页信息
        IPage<Company> p=new Page<>(page,rows);
        //2、分页查询
            //创建条件构造器
        QueryWrapper<Company> queryWrapper=new QueryWrapper<>();
        if (cname!=null&&!"".equals(cname)){
            queryWrapper.like("cname",cname);
        }
        if (status!=null&&!"".equals(status)){
            queryWrapper.eq("status",status);
        }
        if ("asc".equals(ordernumber)){
            System.out.println("升序");
            queryWrapper.orderByAsc("ordernumber");
        }else if ("desc".equals(ordernumber)){
            System.out.println("降序");
            queryWrapper.orderByDesc("ordernumber");
        }
        IPage<Company> companyIPage = baseMapper.selectPage(p,queryWrapper);
        //3、将结果封装到PageResult
        PageResult pageResult=new PageResult<>(companyIPage.getTotal(),companyIPage.getRecords());
        //4、返回
        return pageResult;
    }
}
